<?php
class AdminDepartments
{
	//Public variables available in smarty templates
	public $mDepartmentCount;
	public $mDepartments;
	public $mErrorMessage;
	public $mEditItem;
	public $mLinkToDepartmentsAdmin;
	
	//Private member 
	private $_mAction;
	private $_mActionedDepartmentId;
	
	//Class constructor
	public function __construct()
	{
		//Parse the list with posted variables 
		foreach($_POST as $key => $value)
		{
			//If a submit button was clicked 
			if(substr($key,0,6) == 'submit')
			{
				// Get the postion of last '_'
				$last_underscore =strrpos($key,'_');
				//Get the scope of submit button 
				$this->_mAction =substr($key, strlen('submit_'),$last_underscore - strlen('submit_'));
				// get the department id targeted by submit button 
				$this->_mActionedDepartmentId =substr($key,$last_underscore + 1);
				break;
			}
			$this->mLinkToDepartmentsAdmin =Link::ToDepartmentsAdmin();
			
		}
		
	}
	
	public function init()
	{
		//If adding a new department ...
		if($this->_mAction == 'add_dept')
		{  
			$department_name = $_POST['department_name'];
			$department_description =$_POST['department_description'];
			
			if($department_name == null)
			$this->mErrorMessage ='Department Name Required ';
			
			if($this->mErrorMessage == null)
			{
				Catalog::AddDepartment($department_name, $department_description);
				
				header('Location:'.$this->mLinkToDepartmentsAdmin);
			}
			
		}
		
		// if editing an existing department 
		if($this->_mAction == 'edit_dept')
		$this->mEditItem =$this->_mActionedDepartmentId ;

		//if Updating a department
	    if($this->_mAction == 'update_dept')
		{  
			$department_name = $_POST['name'];
			$department_description =$_POST['description'];
			
			if($department_name == null)
			$this->mErrorMessage ='Department Name Required ';
			
			if($this->mErrorMessage == null)
			{
				Catalog::UpdateDepartment($this->_mActionedDepartmentId,$department_name, $department_description);
				
				header('Location:'.$this->mLinkToDepartmentsAdmin);
			}
			
		}
		
		//If deleting the department 
		if($this->_mAction =='delete_dept')
		{
			$status =Catalog::DeleteDepartment($this->_mActionedDepartmentId) ;
			
			if($status < 0)
			$this->mErrorMessage ='Department not Empty';
			else 
			header('Location '.$this->mLinkToDepartmentsAdmin);
		}
		
		//if editing deaprtments categories 
		if($this->_mAction =='edit_cat')
		{
			header('Location:'.htmlspecialchars_decode(Link::ToDepartmentCategoriesAdmin($this->_mActionedDepartmentId)));
			
			exit();
		}
		
		//Load the list of departments 
		$this->mDepartments =Catalog::GetDepartmentsWithDescriptions();
		$this->mDepartmentCount=count($this->mDepartments);
		
	}
}